<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/30 0030
 * Time: 18:58
 */

namespace api\storage\controller;

use api\storage\model\FeedStationModel;
use api\storage\service\FeedService;
use function Couchbase\defaultDecoder;
use think\Process;
use think\Request;
use api\storage\common\Constant;
use api\storage\service\FeedOutputService;
use think\response\Json;

class FeedController extends AppBaseController
{
    protected $feedServer;
    protected $feedStationModel;
    protected $feedOutputService;

    public function __construct(Request $request = null, FeedService $feedService, FeedStationModel $feedStationModel, FeedOutputService $feedOutputService)
    {
        parent::__construct($request);

        $this->feedServer = $feedService;
        $this->feedStationModel = $feedStationModel;
        $this->feedOutputService = $feedOutputService;

    }

    /**
     * @api {GET} /api/storage/feed/check_feed 1.对工单号的状态进行确认
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  check_feed
     * @apiGroup  feed
     * @apiVersion 0.0.1
     * @apiDescription 传入扫描获取的feed_id,检查该工单的状态，根据状态决定下一步是进入出库还是先进行工单检查
     *
     * @apiParam {string} feedId 料站表号码
     *
     * @apiSuccess {string} code 241
     * @apiSuccess {string} msg 工单正在出库中
     * *@apiSuccess {string} code 242
     * @apiSuccess {string} msg 该工单齐套，是否开始生产
     * @apiSuccess {Array} data 容器信息
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 错误信息
     **/
    public function checkFeed()
    {
        $feedId = input('feedId');
        $result = $this->feedServer->getFeedData($feedId);

        switch ($result['status']) {
            case 0:
                $this->success("工单正在出库中", $result['data'], CONSTANT::FEED_CODE_CHECK_CONTINUE);
                break;
            case 1:
                $this->error("该工单正在生产中");
                break;
            case 2:
                $this->error("该工单已完成");
                break;
            case 3:
                $this->error("工单号不存在");
                break;
            case 4:
                $this->success("进入齐套检查", "", Constant::FEED_CODE_CHECK_BEGIN);
                break;
        }
    }

    /**
     * @api {GET} /api/storage/feed/check_simulation 2.模拟检查
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  check_simulation
     * @apiGroup  feed
     * @apiVersion 0.0.1
     * @apiDescription 传入扫描获取的feed_id,检查其是否齐套，并返回数据
     *
     * @apiParam {string} feedId 料站表号码
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 该工单齐套，是否开始生产
     * @apiSuccess {Array} data 容器信息
     * @apiError   {Error} code 100 料站表的物料号为空
     * @apiError   {Error} code 243 该工单不齐套
     * @apiError  {Error} code 244 所需物料不在库中
     **/
    public function CheckSimulation()
    {
        $feedId = input('feedId');
        $result = $this->feedServer->checkSimulation($feedId);//对工单进行检测
        switch ($result['status']) {
            case 1:
                $this->error('该工单不齐套', $result['data'], Constant::FEED_CEDE_SIMULATION_NOT_ENOUGH);//243
                break;
            case 2:
                $this->error('料站表的物料号为空');
                break;
            case 3:
                $this->error("以下所需物料不在库中", $result['data'], Constant::FEED_CODE_MATERIEL_NOT_EXIST);//244
                break;
            case 4:
                $this->success("工单齐套");
                break;
            case 5:
                $this->error("仓库中的盘料不足，请先拆分盘料。",$result['data'],Constant::FEED_CODE_REEL_NOT_ENOUGH);//245
                break;
        }
    }

    /**
     * @api {post} /api/storage/feed/update_simulation 3.更新模拟数量
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  update_simulation
     * @apiGroup  feed
     * @apiVersion 0.0.1
     * @apiDescription 传入扫描获取的feed_id,检查其是否齐套，并返回数据
     *
     * @apiParam {string} feedId 料站表号码
     *
     * @apiSuccess {string} code 0 成功
     * @apiSuccess {string} msg 该工单齐套开始生产
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100 模拟齐套数量发生变更，请重新模拟
     * @apiError   {Error}msg 100 模拟齐套数量发生变更，请重新模拟
     **/
    public function updateSimulation()
    {
        $feedId = input('feedId');
        $result = $this->feedServer->updateSimulation($feedId);

        switch ($result['status']) {
            case 1:
                $this->error('模拟齐套数量发生变更，请重新模拟');
                break;
            case 2:
                $this->success("更新模拟数量成功");
                break;
        }

    }

    public function moveMateriel()
    {
        $feedId = input('feedId');
        $reelId = input('reelId');
        $result = $this->feedOutputService->moveMateriel($feedId, $reelId);
        switch ($result['status']) {
            case 1:
                $this->error("换料失败，换料阶段已结束");
                break;
            case 2:
                $this->success("换料出库成功");
                break;
            case 3:
                $this->error("该工单不需要此物料");
        }
    }

    /**
     * @api {post} /api/storage/feed/refresh 刷新页面
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  refresh
     * @apiGroup  feed
     * @apiVersion 0.0.1
     * @apiDescription 刷新页面
     *
     * @apiParam {string} feedId 料站表号码
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 100
     **/
    public function refresh()
    {
        $feedId = input('feedId');
        $result = $this->feedOutputService->refresh($feedId,$this->getUsername());
        switch ($result['status']) {
            case 1:
                $this->success("进入移料流程", $result['data']);
                break;
            case 2:
                $this->success("进入货架出库流程", $result['data']);
                break;
            case 3:
                $this->success("进入机器减料流程", $result['data']);
                break;
            case 4:
                $this->error("出库流程已结束");
                break;
        }
    }

    /**
     * @api {post} /api/storage/feed/change_process 更改当前流程
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  change_process
     * @apiGroup  feed
     * @apiVersion 0.0.1
     * @apiDescription 出库时询问是否从中转区获取物料
     *
     * @apiParam {string} feedId 料站表号码
     * @apiParam {int}  process 要进入的流程1:换枪流程 2:货架出库流程 3,机台剪料出库流程
     *
     * 如果选择否，则会调用这个接口，将process从流程1切换成流程2，并且推各站位物料的首盘。
     * 选择是，进行中转站上料，结束以后也会切换成这个接口
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 100
     **/
    public function changeProcess()
    {
        $feedId = input("feedId");
        $process = input("process");//要进入的流程
        $result = $this->feedOutputService->changeProcess($feedId, $process ,$this->getUsername());//切换当前流程，进入下一流程

        switch ($result['data']['process']) {
            case 1:
                $processMsg = "换枪出库";
                break;
            case 2:
                $processMsg = "货架出库";
                break;
            case 3:
                $processMsg = "机台减料出库";
                break;
            default:
                $processMsg = "changeProcess出错";
        }

        switch ($result['status']) {
            case 1:
                $this->success("进入" . $processMsg . "流程", $result['data']);
                break;
            case 2:
                $this->success("流程已被其他PDA改变", $result['data']);
                break;
            case 3:
                $this->error("当前出库流程已结束");
                break;
        }
    }

    /**
     * @api {post} /api/storage/feed/output_from_shelve
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  output_from_shelve
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 将扫描的物料出库
     *
     * @apiParam {string} feedId 料站表号码
     * @apiParam {int}  process 要进入的流程1:换枪流程 2:货架出库流程 3,机台剪料出库流程
     * @apiParam {string}  reelId  盘料上的reelId
     * 对扫描的物料进行出库
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 出库成功
     * @apiSuccess {Array} data
     * @apiError   {Error} code 252
     * @apiError   {Error}msg 出库失败 根据数据刷新页面
     **/
    public function outputFromShelve()
    {
        $feedId = input("feedId");
        $process = input("process");
        $reelId = input("reelId");
        $result = $this->feedOutputService->outputFromShelve($feedId, $process, $reelId, $this->getNickname());
        switch ($result['status']) {
            case 1:
                $this->success("出库成功", $result['data']);
                break;
            case 2:
                $this->error("流程已被其他PDA改变", $result['data'],Constant::FEED_OUTPUT_SHELVE_ERROR);
                break;
            case 3:
                $this->error("该reel不存在仓库中", $result['data'],Constant::FEED_OUTPUT_SHELVE_ERROR);
                break;
            case 4:
                $this->error("出库失败，请联系维护人员。");
                break;
            case 5:
                $this->error("该物料是错料。");
                break;
            case 6:
                $this->success('全部出库完毕，流程已结束',"",Constant::FEED_OUT_PUT_IS_OVER);
                break;
        }
    }

    /**
     * @api {post} /api/storage/feed/mismatch_return
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  mismatch_return
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 错料归还接口
     *
     * @apiParam {string}  process 要进入的流程1:换枪流程 2:货架出库流程 3,机台剪料出库流程
     * @apiParam {string}  reelId  盘料上的reelId
     * 对扫描的错料进行归还
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 扫描了错误的物料，请扫码归还
     * @apiSuccess {Array} data
     * @apiError   {Error} code 100
     * @apiError   {Error}msg 100
     **/
    public function mismatchReturn(){
        $process = input('process');
        $reelId = input('reelId');
        $storageUuid = input('storageUuid');
        $result = $this -> feedOutputService->mismatchReturn($process,$reelId);
        switch ($result['status']){
            case 1:
                $this->success("扫描了错误的物料，请扫码归还",$result['data']);
                break;
            case 2:
                $this->error("扫描了错误的物料，请直接归还");
                break;
        }

    }
//    public function getOutputData(){//当前处于出库的什么状态，以及准备要出库的数据
//        $feedId = input('feedId');
//        $result = $this->
//        switch ($result['status']){
//            case 1:
//                $this->error("出库流程已结束");
//                break;
//            case 2:
//                $this->success("进入".."流程中");
//        }
//
//
//    }


}